package com.bysreg;

import java.util.LinkedList;

public class PreviewContainer {
    private LinkedList<Preview> previews;
    private int totallength;
        
    class Bold {
        int index;
        int type;

        public Bold(int index, int type) {
            this.index = index;
            this.type = type;
        }
    }
    
    class Preview {
        int idxHigh;
        int idxLow;
        String spreview;
        int length;
        LinkedList<Bold> bolds;
        
        public Preview(int idxLow, int idxHigh, String spreview, int idx, int count) {
            this.idxHigh = idxHigh;
            this.idxLow = idxLow;
            this.spreview = spreview;
            addBold(idx, count);
        }                
        
        public final void addBold(int index, int count) {
            for(int i=0;i<bolds.size();i++) {
                if(index<bolds.get(i).index) {
                    bolds.add(i,new Bold(index,0));//open bold tag                    
                }
                if(count<bolds.get(i).index) {
                    bolds.add(i,new Bold(index,1));//close bold tag
                }
            }                                    
        }    
    }
    
    public PreviewContainer() {
        previews = new LinkedList<Preview>();
    }
    
    public int getPreviewCount() {
        return previews.size();
    } 
    
    public int getPreviewLength() {
        return totallength;
    }
    
    public String processPreview() {
        StringBuilder res = new StringBuilder();
        for(Preview preview : previews) {                        
            char[] temp = preview.spreview.toCharArray();
            for(int i=0;i<preview.spreview.length();i++) {
                
                res.append(temp[i]);
            }            
        }
        return res.toString();
    }
    
    private boolean alreadyExistInPreview(int idx, int count) {
       for(Preview preview : previews) {
            if(preview.idxLow >idx && preview.idxHigh <count+idx-1) {
                preview.addBold(idx, count);
                return true;
            }
       } 
       return false;
    }
    
    public void addPreview(char[] s, int idx, int count) {
        if(alreadyExistInPreview(idx, count)) {
            return;
        }
        int idxLow = 0;
        int idxHigh = 0;
        //jika kata kunci ditemukan di kalimat awal file
        int i;       
        StringBuilder apreview = new StringBuilder();
        if(idx==0) {//berada di ujung awal teks
            i=idx;   
            idxLow=i;
            while(i<idx+Crawler.MAX_A_MATCH_PREVIEW_CHAR && i<s.length  && getPreviewLength()+apreview.length()<Crawler.MAX_PREVIEW_CHAR) {                
                apreview.append(s[i]);                
                i++;
            }
            idxHigh=i-1;
        }else if(idx+count==s.length) { //berada di ujung akhir teks            
            int temp = apreview.length();
            i=idx+count-1;
            idxHigh=i;
            while(i>=idx-(Crawler.MAX_A_MATCH_PREVIEW_CHAR-count) && i>=0  && getPreviewLength()+apreview.length()<Crawler.MAX_PREVIEW_CHAR) {                                
                apreview.insert(temp, s[i]);
                i--;
            }
            idxLow=i+1;
        }else{//berada di tengah teks
            //ambil dari kiri dulu abis itu ke kanan
            i=idx+count-(Crawler.MAX_A_MATCH_PREVIEW_CHAR/2);            
            if((Crawler.MAX_A_MATCH_PREVIEW_CHAR/2)>(Crawler.MAX_PREVIEW_CHAR-getPreviewLength())) {//kondisi dimana jumlah karakter tersisa untuk preview lebih kecil dari yang diinginkan
                i = idx+count-(Crawler.MAX_PREVIEW_CHAR-getPreviewLength());                
            }
            if(i<0) {
                i=0;
            }
            idxLow=i;
            while(i<idx+(Crawler.MAX_A_MATCH_PREVIEW_CHAR/2) && i<s.length && getPreviewLength()+apreview.length()+3<Crawler.MAX_PREVIEW_CHAR) {                
                apreview.append(s[i]);
                i++;
            }
            idxHigh=i-1;
        }
        previews.add(new Preview(idxLow,idxHigh,apreview.toString(),idx,count));        
        totallength += apreview.length();
    }
        
}
